local budao = fk.CreateSkill{
  name = "rainveil__budao",
}

Fk:loadTranslationTable{
  ["rainveil__budao"] = "布道",
  [":rainveil__budao"] = "你的体力值变化后，你可以将至少一张牌置于牌堆顶，然后令一名角色将手牌数摸至X张(X为本回合弃牌堆的花色数)。",
  ["#rainveil__budao"] = "布道：将任意张牌置于牌堆顶",
  ["#rainveil__budao_change"] = "布道：选择一名角色将其手牌摸至 %arg 张",

  ["$rainveil__budao1"] = "吾承天道法，闭其凶恶之路，开天太平之阶。",
  ["$rainveil__budao2"] = "幸欲报天地之功而得寿者，努力信道勿懈。",
}

budao:addEffect(fk.HpChanged, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(budao.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 999,
      prompt = "#rainveil__budao",
      include_equip = true,
      skill_name = budao.name,
    })
    if #cards == 0 then return end

    event:setCostData(self, {cards = cards})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    cards = room:askToArrangeCards(player, {
      skill_name = budao.name, 
      box_size = 0,
      min_limit = {1},
      max_limit = {#cards},
      card_map = {"Top",cards},
      free_arrange = true,
      prompt = "#rainveil__budao"
    })[1]
    room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, budao.name, nil, nil, player)
    local suits = {}
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              table.insertIfNeed(suits, Fk:getCardById(info.cardId).suit)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    local available_players = table.filter(room:getAlivePlayers(false), function (p)
      return p:getHandcardNum() < #suits
    end)
    if #available_players == 0 then return end
    local target = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = available_players,
      skill_name = budao.name,
      prompt = "#rainveil__budao_change:::"..#suits,
      cancelable = false
    })[1]
    if not target then return end
    target:drawCards(#suits - target:getHandcardNum(), budao.name)
  end
})

return budao